/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.spring.manager.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Group;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Role;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.GroupRoleManager;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.RoleDAO;

/**
 * dumb implementation of {@link GroupRoleManager} that give the first role by
 * id to all {@link Group}.
 */
public class DumbGroupRoleManager
    implements GroupRoleManager
{

  /**
   * the {@link RoleDAO} to use.
   */
  private RoleDAO roleDAO;



  /**
   * returns the {@link Set} of {@link Role} associated to a {@link Group}.
   * 
   * @param group
   *          the group to consider
   * @return the {@link Set} of {@link Role} associated to a {@link Group}
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.GroupRoleManager#getRoles(com.genia.toolbox.projects.toolbox_basics_project.bean.model.Group)
   */
  public Set<Role> getRoles(Group group)
      throws PersistenceException
  {
    HashSet<Role> res = new HashSet<Role>();
    List<Role> allRoles = getRoleDAO().getAll();
    Role currentRole = null;
    for (Role role : allRoles) {
      if (currentRole == null || currentRole.getIdentifier() > role.getIdentifier()) {
        currentRole = role;
      }
    }
    if (currentRole != null) {
      res.add(currentRole);
    }
    return res;
  }



  /**
   * returns the {@link Set} of {@link Role} associated to a {@link Set} of
   * {@link Group}s.
   * 
   * @param groups
   *          the groups to consider
   * @return the {@link Set} of {@link Role} associated to a {@link Set} of
   *         {@link Group}
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.GroupRoleManager#getRoles(java.util.Set)
   */
  public Set<Role> getRoles(Set<Group> groups)
      throws PersistenceException
  {
    HashSet<Role> res = new HashSet<Role>();
    for (Group group : groups) {
      res.addAll(getRoles(group));
    }
    return res;
  }



  /**
   * getter for the roleDAO property.
   * 
   * @return the roleDAO
   */
  public RoleDAO getRoleDAO()
  {
    return roleDAO;
  }



  /**
   * setter for the roleDAO property.
   * 
   * @param roleDAO
   *          the roleDAO to set
   */
  public void setRoleDAO(RoleDAO roleDAO)
  {
    this.roleDAO = roleDAO;
  }

}
